package com.geuso.xingdutch.words;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.geuso.xingdutch.R;
import com.geuso.xingdutch.util.SoundUtil;

public class WordsListAdapter extends ArrayAdapter<Word>{

	private int layout_resource;
	
	public WordsListAdapter(Context context, int resource, ArrayList<Word> words){
		super(context, resource, words);
		this.layout_resource = resource;
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		// assign the view we are converting to a local variable
		View v = convertView;

		// first check to see if the view is null. if so, we have to inflate it.
		// to inflate it basically means to render, or show, the view.
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(layout_resource, null);
		}

		Word w = getItem(position);
		if (w != null){
			TextView dutch = (TextView) v.findViewById(R.id.w_dutch);
			TextView dutch_pronounciation = (TextView) v.findViewById(R.id.w_dutch_pronounce);
			TextView english = (TextView) v.findViewById(R.id.w_english);
			TextView chinese = (TextView) v.findViewById(R.id.w_chinese);
			ImageView play_sound = (ImageView) v.findViewById(R.id.w_play_sound);
			
			

			if (dutch != null)
				dutch.setText(w.getDutch());
			
			if (dutch_pronounciation != null) 
				dutch_pronounciation.setText(w.getDutch_pronounciation());
			
			if (english != null)
				english.setText(w.getEnglish());

			if (chinese != null)
				chinese.setText(w.getChinese());
			
			if (play_sound != null){
				if (w.getSound_file() > 0){
					play_sound.setVisibility(View.VISIBLE);
					SoundUtil.setOnClickPlaySound(play_sound, w.getSound_file());
				}
				else play_sound.setVisibility(View.INVISIBLE);
			}
			
		}
		
		
		return v;
	}
	
	
	
}
